package easycloud.ecs.web.controller;

import java.util.HashMap;
import java.util.Map;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefaults;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import easycloud.ecs.common.domain.User;
import easycloud.ecs.web.exception.DomainNotFoundException;
import easycloud.ecs.web.service.UserService;

@Controller
@RequestMapping("user")
public class UserController extends BaseController {
	private static final Log logger = LogFactory.getLog(UserController.class);

	@Inject
	private UserService userService;

	@RequestMapping(value = "checkEmail", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> checkEmail(
			@RequestParam(value = "email", required = false) String email) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		boolean result = !userService.isEmailExist(email);
		resultMap.put("state", result);
		if (result) {
			resultMap.put("message", "Email验证通过!");
		} else {
			resultMap.put("message", "Email已经存在!");
		}
		return resultMap;
	}

	@RequestMapping(value = "detail")
	public ModelAndView detail(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("user.detail");
		User user = getUser(request);
		mav.addObject("user", user);
		return mav;
	}

	@RequestMapping(value = "list", method = RequestMethod.GET)
	public ModelAndView listSetup(@PageableDefaults Pageable pageable) {
		ModelAndView mav = new ModelAndView("user.list");
		Page<User> users = userService.findUsers(pageable);
		mav.addObject("page", users);
		return mav;
	}

	@RequestMapping(value = "{userId}")
	public ModelAndView profile(@PathVariable("userId") Integer userId) {
		ModelAndView mav = new ModelAndView("user.profile");
		User user = userService.getUser(userId);
		if (user == null) {
			throw new DomainNotFoundException(User.class, userId);
		}

		mav.addObject("user", user);
		return mav;
	}

	@RequestMapping(value = "signup", method = RequestMethod.POST)
	public ModelAndView signupProcess(@ModelAttribute("user") @Valid User user,
			BindingResult userBindingResult) {
		ModelAndView mav = new ModelAndView("common.msg");
		if (userBindingResult.hasErrors()) {
			mav.setViewName("user.signup");
			return mav;
		}
		try {
			userService.register(user, User.Role.NORMAL);
			mav.addObject("msg", "注册成功");
		} catch (Exception e) {
			mav.addObject("msg", "注册失败");
		}
		return mav;
	}

	@RequestMapping(value = "signup", method = RequestMethod.GET)
	public ModelAndView signupSetup() {
		ModelAndView mav = new ModelAndView("user.init");
		mav.addObject("user", new User());
		return mav;
	}
}
